﻿Imports System.Data.OleDb, System.Drawing

Public Class AddHotel
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub bt_AddHotel_Click(sender As Object, e As EventArgs) Handles bt_AddHotel.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
        Dim SqlString As String = "Insert Into Hotels(HotelName, StarRating, Location , HotelUrl,Description,HotelPicture, ContactDetails, Visitor) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim myGUID, myGUID2 As New System.Guid
        myGUID = Guid.NewGuid()
        myGUID2 = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"
        Dim newFileName2 = myGUID2.ToString() + ".jpg"

        If F_Picture.HasFile Then
            F_Picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
            Using OriginalImage = Image.FromFile(Request.MapPath("pictures") & "/" & newFileName)
                Using ResizedImage As New Bitmap(OriginalImage, 500, 500)
                    ResizedImage.Save(Request.MapPath("pictures") & "/" & newFileName2, Drawing.Imaging.ImageFormat.Jpeg)

                End Using
            End Using
        End If
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_HotelName.Text)
        cmd.Parameters.AddWithValue("@f2", rbl_StarRating.SelectedValue)
        cmd.Parameters.AddWithValue("@f3", tb_Location.Text)
        cmd.Parameters.AddWithValue("@f4", tb_HotelUrl.Text)
        cmd.Parameters.AddWithValue("@f5", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f6", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f7", tb_ContactDetails.Text)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("confirmation.aspx")
    End Sub
End Class